package com.lsm.sm.util;

import java.security.MessageDigest;

public class MD5Util {
	private static char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
			'9', 'A', 'B', 'C', 'D', 'E', 'F' };
	private static MessageDigest messageDigest;
	
	public static String encode(String pwd){
		try{
			messageDigest = MessageDigest.getInstance( "MD5" );
		}catch( Exception e ){
			System.out.println( "Error from " + MD5Util.class.getName() + "\n" + e.getMessage());
			throw new RuntimeException( e );
		}
		messageDigest.update( pwd.getBytes() );
		return toHex( messageDigest.digest() );
	}
	
	public static String toHex( byte[] buffer ){
		char[] cs = new char[2*buffer.length];
		for( int i=0; i<buffer.length; i++ ){
			cs[i*2] = hex[(buffer[i] & 240)>>4];
			cs[i*2+1]= hex[buffer[i]&15];
		}
		return new String( cs );
	}
	
	public static void main(String[] args) {
		System.out.println( encode( "shi492788802" ) );
	}
}
